/**
 * Created by Administrator on 2017/7/4 0004.
 */
var dataApi = require('../app/config/dataApi');
var userDao = require('../dao/userDao');

var rotateItem = function (req,res) {
    var flag = parseInt(req.headers.flag);
    var uid = req.headers.uid;
    var sid = req.headers.sid;

    if(!flag || !uid){
        res.send({
            code:1,
            info:'参数缺失'
        });
    }
    var item,cost;

    if(flag == 1){
        cost = 5;
    }else if(flag == 2){
        cost = 20;
    }else {
        cost = 50;
    }

    userDao.getUserById(uid,null,null,function (err,user) {
        if(err){
            res.send({
                code:1,
                info:'未知用户'
            });
            return;
        }
        if(user.role == 1){
            res.send({
                code:1,
                info:'用户已被封禁'
            });
            return;
        }
        if(user.coin < cost){
            res.send({
                code:1,
                info:'金币不足'
            });
            return;
        }


        userDao.userAction(uid,flag,function (err,num) {            //获取转盘操作数据,num即第几次转flag的转盘,从0开始
            item =  getItemId(flag,num,uid,sid,cost,user);
            res.send({
                code:0,
                item:item
            });
        });
    })


};


var getItemId = function (flag,num,uid,sid,cost,user) {

    /*if(num == 2 || num == 4){
        num -= 3;
    }else {
        num = -1;
    }*/

    var item = 1;
    var coin = 0;

    switch (flag){
        case 1:                //small:前两次赢,第三次开始kill
            if(num < 2){       //win:0,1
                coin = 9;
                item = 2;

            }else if(num >1 && num < 4){  //kill:2,3
                coin = 1;
                item = 1;
            }else {
                if(Math.random() < 0.4){   //win
                    coin = 9;
                    item = 2;
                }else {
                    coin = 1;
                    item = 1;
                }
            }
            break;
        case 2:                //mid:第二次赢,其余kill
            if(num == 1){
                item = 5;
                coin = 35;

            }else if(num == 0){
                item = Math.floor(Math.random() * 3 + 1);
                if(item == 1){
                    coin = 5;
                }else if(item == 2){
                    coin = 10;
                }else if(item == 3){
                    coin = 15;
                }
            }else {
                if(Math.random() < 0.4){        //win
                    item = Math.floor(Math.random() * 3 + 4);
                    if(item == 4){
                        coin = 30;
                    }else if(item == 5){
                        coin = 35;
                    }else if(item == 6){
                        coin = 50;
                    }
                }else {
                    item = Math.floor(Math.random() * 3 + 1);
                    if(item == 1){
                        coin = 5;
                    }else if(item == 2){
                        coin = 10;
                    }else if(item == 3){
                        coin = 15;
                    }
                }
            }
            break;
        case 3:                 //big:第二次赢,其余kill
            if(num == 1){
                item = 7;
                coin = 70;
            } else if(num == 0){
                item = Math.floor(Math.random() * 4 + 1);
                if(item == 1){
                    coin = 10;
                }else if(item == 2){
                    coin = 20;
                }else if(item == 3){
                    coin = 30;
                }else if(item == 4){
                    coin = 40;
                }
            }else{
                if(Math.random() < 0.4){        //win
                    item = Math.floor(Math.random() * 4 + 5);
                    if(item == 5){
                        coin = 50;
                    }else if(item == 6){
                        coin = 60;
                    }else if(item == 7){
                        coin = 70;
                    }else if(item == 8){
                        coin = 80;
                    }
                }else {
                    item = Math.floor(Math.random() * 4 + 1);
                    if(item == 1){
                        coin = 10;
                    }else if(item == 2){
                        coin = 20;
                    }else if(item == 3){
                        coin = 30;
                    }else if(item == 4){
                        coin = 40;
                    }
                }
            }
            break;
    }

    userDao.increaseUserCoin(uid,coin - cost,function (){});   // handle coin
    userDao.recordUserAction(uid,sid,flag,item,coin);           // record action
    //发放佣金
    if(user.referee1){
        userDao.issueCommission(uid,user.referee1, function (err,data) {
            if(data && data.totalFee && data.totalComi ){
                if(data.totalFee/1000 < data.totalComi){
                    if(user.referee1){
                        userDao.addCommission(user.referee1,cost * 0.1,uid);
                    }
                }
            }
        });
    }

    if(user.referee2){
        userDao.issueCommission(uid,user.referee2, function (err,data) {
            if(data && data.totalFee && data.totalComi ){
                if(data.totalFee/2000 < data.totalComi){
                    if(user.referee1){
                        userDao.addCommission(user.referee2,cost * 0.05,uid);
                    }
                }
            }
        });
    }

    return item;
};

exports.rotateItem = rotateItem;
